import {
  Directive,
  ElementRef,
  HostListener,
  Inject,
  Input,
  OnInit,
  OnChanges,
  Output
} from "@angular/core";
import { LocaleService, TranslationService, Language } from "angular-l10n";

@Directive({
  selector: "[appTimeAgo]"
})
export class TimeAgoDirective implements OnInit, OnChanges {
  el: ElementRef;
  @Input()
  ago: number;
  @Input()
  now: number;

  constructor(el: ElementRef, public locale: LocaleService) {
    this.el = el;
    // this.ago = this.getUnixNow();
    // this.now = this.getUnixNow();
  }

  ngOnInit() {
    // this.format(this.el.nativeElement, {
    //   ago: this.ago,
    //   now: this.now,
    //   locale: this.locale.getCurrentLocale()
    // });
    this.addListen();
  }

  addListen() {
    // this.locale.defaultLocaleChanged.subscribe(() => {
    //   switch (this.locale.getCurrentLocale()) {
    //     case "en-US":
    //       this.locale.setCurrentCurrency("USD");
    //       break;
    //     case "zh-CN":
    //       this.locale.setCurrentCurrency("CNY");
    //       break;
    //     case "de-DE":
    //       this.locale.setCurrentCurrency("EUR");
    //       break;
    //   }
    // });
  }

  ngOnChanges() {
    const ago = this.ago;
    const now = this.now;
    const locale = this.locale.getCurrentLocale();
    this.format(this.el.nativeElement, {
      ago,
      now,
      locale
    });
  }

  // 移动到utitls

  getUnixNow() {
    return Number(Number(new Date().getTime() / 1000).toFixed(0));
  }

  format(el, m) {
    const ago = m.ago;
    const now = m.now;
    const locale = m.locale;
    const i18n = {
      "en-US": {
        m: "minutes ago",
        h: "hours ago",
        d: "days ago",
        mon: "months ago",
        y: "years ago",
        now: "just now"
      },
      "zh-CN": {
        m: "分钟以前",
        h: "小时以前",
        d: "天以前",
        mon: "月以前",
        y: "年以前",
        now: "刚刚"
      },
      "de-DE": {
        m: "分前",
        h: "時間前",
        d: "日前",
        mon: "ヶ月前",
        y: "年前",
        now: "ちょうど今"
      }
    };

    const lang = i18n[locale];

    const stpAgo = ago;
    const stpNow = now;

    const stpDiffer = stpNow - stpAgo;

    console.log(stpDiffer, 999999);

    // 1 s
    const s1 = 1000;
    // 1 m
    const m1 = s1 * 60;

    // 1 hour
    const h1 = m1 * 60;

    // 1 day
    const d1 = h1 * 24;

    // 1 month
    const mon1 = d1 * 30;

    // 1 year
    const y1 = mon1 * 12;
    if (stpDiffer < h1) {
      // min
      const min = stpDiffer / m1;
      this.el.nativeElement.innerHtml = Math.floor(min)
        ? `${Math.floor(min)} ${lang.m}`
        : lang.now;
      this.el.nativeElement.setAttribute("aaa", "bbbb");
      // this.el.nativeElement.innerText = "dslkfjldksjfls";
      console.log(this.el.nativeElement);
      return;
    }

    if (stpDiffer < d1) {
      // hour
      const hour = stpDiffer / h1;
      el.innerText = `${Math.floor(hour)} ${lang.h}`;
      return;
    }

    if (stpDiffer < mon1) {
      // day
      const day = stpDiffer / d1;
      el.innerText = `${Math.floor(day)} ${lang.d}`;
      return;
    }

    if (stpDiffer < y1) {
      // month
      const month = stpDiffer / mon1;
      el.innerText = `${Math.floor(month)} ${lang.mon}`;
      return;
    }

    // year
    const year = stpDiffer / y1;
    el.innerText = `${Math.floor(year)} ${lang.y}`;
  }
}
